<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

function create_dir( $dir )
{
	$dir = preg_replace( "|^(.+?)/*$|", "\\1", $dir );
	if( FTP_ENABLE )
	{
		require_once( SUNCMS.'include/librarys/ftp.class.php' );
		$ftp = new Ftp();
	}
	if( !is_dir( $dir ) )
	{
		$temp = explode( '/',$dir );
		$cur_dir = "";
		for( $i = 0; $i < count( $temp ); $i++ )
		{
			$cur_dir .= $temp[$i]."/";
			if( !is_dir( $cur_dir ) )
			{
				if( FTP_ENABLE )
				{
					$cur_dir = str_replace( SITE_PATH, '/', $cur_dir );
					$ftp->mkdir( $cur_dir, 0777 );
				}
				else
				{
					@mkdir( $cur_dir, 0777 );
				}
			}
        }
        return is_dir( $dir )?true:false;
   	}
}

function rename_dir( $old_dir, $new_dir )
{
	$old_dir = preg_replace( "|^(.+?)/*$|", "\\1", $old_dir );
	$new_dir = preg_replace( "|^(.+?)/*$|", "\\1", $new_dir );
	if( FTP_ENABLE )
	{
		require_once( SUNCMS.'include/librarys/ftp.class.php' );
		$ftp = new Ftp();
	}
	if( is_dir( $old_dir ) )
	{
		if( FTP_ENABLE )
		{
			$old_dir = str_replace( SITE_PATH, '/', $old_dir );
			$new_dir = str_replace( SITE_PATH, '/', $new_dir );
			$ftp->rename( $old_dir, $new_dir );
		}
		else
		{
			@rename( $old_dir, $new_dir );
		}
   	}
	else
	{
		return create_dir( $new_dir )?true:false;
	}
}

//删除文件
function delete_files( $path, $del_dir = false, $level = 0 )
{	
	//除去后面的斜杠
	$path = preg_replace( "|^(.+?)/*$|", "\\1", $path );
	if( !$current_dir = @opendir( $path ) )
	{
		return;
	}
	while( ( $filename = @readdir( $current_dir ) ) !== false )
	{
		if( $filename != "." && $filename != ".." )
		{
			if( is_dir( $path.'/'.$filename ) )
			{
				// 忽略空目录
				if( substr( $filename, 0, 1 ) != '.' )
				{
					delete_files( $path.'/'.$filename, $del_dir, $level + 1 );
				}
			}
			else
			{
				@unlink( $path.'/'.$filename );
			}
		}
	}
	@closedir($current_dir);
	if( $del_dir == true )
	{
		@rmdir( $path ); 
	}
}

//获取目录文件信息
function get_dir_file_info( $source_dir, $include_path = false, $_recursion = false )
{
	$_filedata = array();
	$relative_path = $source_dir;
	if( $fp = @opendir( $source_dir ) )
	{
		if( $_recursion === false )
		{
			$_filedata = array();
			$source_dir = rtrim( realpath( $source_dir ), DIRECTORY_SEPARATOR ).DIRECTORY_SEPARATOR;
		}
		while ( ( $file = readdir( $fp ) ) !== false )
		{
			if( @is_dir( $source_dir.$file ) && strncmp( $file, '.', 1 ) !== 0 )
			{
				 get_dir_file_info( $source_dir.$file.DIRECTORY_SEPARATOR, $include_path, true );
			}
			elseif( strncmp( $file, '.', 1 ) !== 0 )
			{
				$_filedata[$file] = get_file_info( $source_dir.$file );
				$_filedata[$file]['relative_path'] = $relative_path;
			}
		}
		return $_filedata;
	}
	else
	{
		return false;
	}
}

//获取文件信息
function get_file_info( $file, $returned_values = array('name', 'server_path', 'size', 'date') )
{
	if( !file_exists( $file ) )
	{
		return false;
	}
	if( is_string( $returned_values ) )
	{
		$returned_values = explode( ',', $returned_values );
	}
	foreach ( $returned_values as $key )
	{
		switch ( $key )
		{
			case 'name':
				$fileinfo['name'] = substr(strrchr($file, '/'), 1);
				break;
			case 'server_path':
				$fileinfo['server_path'] = $file;
				break;
			case 'size':
				$fileinfo['size'] = filesize($file);
				break;
			case 'date':
				$fileinfo['date'] = filectime($file);
				break;
			case 'readable':
				$fileinfo['readable'] = is_readable($file);
				break;
			case 'writable':
				//在IIS上有可能不正确 fileperms()
				$fileinfo['writable'] = is_writable($file);
				break;
			case 'executable':
				$fileinfo['executable'] = is_executable($file);
				break;
			case 'fileperms':
				$fileinfo['fileperms'] = fileperms($file);
				break;
		}
	}
	return $fileinfo;
}

/**读取指定目录下的文件**/
function sread_dir( $dir, $ext='' )
{
	$filearr = array();
	if( is_dir( $dir ) )
	{
		$filedir = dir( $dir );
		while( ( $entry = $filedir->read() ) !== false )
		{
			if( !empty( $ext ) )
			{
				if ( strtolower( get_fileext( $entry ) ) == strtolower( $ext ) )
				{
					$filearr[] = $entry;
				}
			}
			else
			{
				if( $entry != '.' && $entry != '..' )
				{
					$filearr[] = $entry;
				}
			}
		}
		$filedir->close();
	}
	return $filearr;
}